using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Collections;
using System.Data;
using System.IO;
using MyClever.Lib;

namespace MyClever.DefaultPlugins
{
    /// <summary>
    /// Get all expected files from all desired folders, using a recursive algorithm. 
    /// </summary>
    public class SearchFiles : MissionPlugin
    {
        [Package(Direction.Input, "Defines the directories you like to search through.")]
        enum Paths
        {
            [Column(typeof(string), "Path where you like to search the expected files.",
               Validation = "IsPath")]
            Path,
            [Column(typeof(bool), "if true, all subfolders will be ignored.",
             DefaultValue = "false")]
            IgnoreSubDirectories
        }

        [Package(Direction.Input, "Defines the extensions of the files you like to search for.")]
        enum FileSettings
        {
            [Column(typeof(string), "Extension of the file you search for. (example: bmp, pdf, txt)",
                Validation = "hasminlength{1}")]
            Extension
        }

        [Package(Direction.Output, "Defines all found files with the expected data.")]
        enum Output
        {
            [Column(typeof(string), "The expected result of the filesearch.")]
            Filename
        }

        [Plugin(PluginDevStatus.Production, "Get all expected files from all desired folders, using a recursive algorithm.")]
        public SearchFiles()
        {
        }

        public override void Initialize()
        {
            //This is the first Function called by the host...
            //Put anything needed to start with here first
        }

        public override void Dispose()
        {
            //Put any cleanup code in here for when the program is stopped
        }


        protected override void Work()
        {
            DataTableReader pathsPackage = this.GetDataReader(typeof(Paths));
            while (pathsPackage.Read())
            {
                this.AddNewLoggingMessage(typeof(Paths), String.Format("search in path: {0}", pathsPackage[Paths.Path.ToString()].ToString()));
                
                SearchOption searchOption = SearchOption.AllDirectories;
                if (true == Convert.ToBoolean(pathsPackage[Paths.IgnoreSubDirectories.ToString()]))
                {
                    searchOption = SearchOption.TopDirectoryOnly;
                }
                
                DataTableReader fileSettingsPackage = this.GetDataReader(typeof(FileSettings));
                while (fileSettingsPackage.Read())
                {
                    this.AddNewLoggingMessage(typeof(FileSettings), String.Format("look for file with extension: {0}", fileSettingsPackage[FileSettings.Extension.ToString()].ToString()));
                    
                    string searchPattern = "*." + fileSettingsPackage[FileSettings.Extension.ToString()].ToString();
                    
                    ArrayList files = new ArrayList();
                    files.AddRange(Directory.GetFiles(pathsPackage[Paths.Path.ToString()].ToString(), searchPattern, searchOption));
                    for (int fileID = 0; fileID < files.Count; fileID++)
                    {
                        DataRow newRow = this.CreateNewRowFromPackage(typeof(Output));
                        newRow[Output.Filename.ToString()] = files[fileID].ToString();
                        this.AddNewRowToPackage(typeof(Output), newRow);
                    }
                    files.Clear();
                }
            }
        }
    }
}
